package orbito.util;

public class Stopwatch {
	
	// The time of the last call to getTimeElapsed.
	long lastFrameTime;
	
	// The time that has passed since the last call to getTimeElapsed, in seconds.
	double frameElapsedTime;
	
	/**
	 * Constructs a new stopwatch, and starts a new lap.
	 */
	public Stopwatch() {
		this.reset();
	}
	
	/**
	 * Resets this stopwatch, starting a new lap.
	 */
	public void reset() {
		this.lastFrameTime = System.nanoTime();
	}
	
	/**
	 * Gets the passed time, and starts a new lap.
	 * 
	 * @return The elapsed time since the last call, in seconds.
	 */
	public double getElapsedTime() {
		long frameTime = System.nanoTime();
		frameElapsedTime = (frameTime - lastFrameTime) / 1e9;
		lastFrameTime = frameTime;
		return frameElapsedTime;
	}
	
	/**
	 * Gets the passed time, without starting a new lap.
	 * 
	 * @return The elapsed time since the last call to getElapsedTime, in seconds.
	 */
	public double peekElapsedTime() {
		frameElapsedTime = (System.nanoTime() - lastFrameTime) / 1e9;
		return frameElapsedTime;
	}
}
